package com.jmx.yp.struts.group;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.*;
import net.sf.json.util.PropertyFilter;
import net.sf.json.JSONObject;

import com.jmx.yp.common.SysConstants;
import com.jmx.yp.entity.GroupEmployee;
import com.jmx.yp.entity.GroupInfo;
import com.jmx.yp.exception.ErrorCode;
import com.jmx.yp.exception.HandlerException;
import com.jmx.yp.service.group.IGroupinfoService;
import com.opensymphony.xwork2.ActionContext;

public class GroupinfoAction {

    //service类   spring action.xml配置spring管理
	private IGroupinfoService groupservice;
	public IGroupinfoService getGroupservice() {
		return groupservice;
	}
	public void setGroupservice(IGroupinfoService groupservice) {
		this.groupservice = groupservice;
	}
	
	/**当前组织**/
	private Long currentGroupId;  //当前组织ID
	private String currentGroupName;  //当前组织名称
	private Integer currentGroupLvl;  //当前组织级别
	private String currentGroupDir;  //当前组织路径
	
	public Long getCurrentGroupId() {
		return currentGroupId;
	}
	public void setCurrentGroupId(Long currentGroupId) {
		this.currentGroupId = currentGroupId;
	}
	public String getCurrentGroupName() {
		return currentGroupName;
	}
	public void setCurrentGroupName(String currentGroupName) {
		this.currentGroupName = currentGroupName;
	}
	public Integer getCurrentGroupLvl() {
		return currentGroupLvl;
	}
	public void setCurrentGroupLvl(Integer currentGroupLvl) {
		this.currentGroupLvl = currentGroupLvl;
	}
	public String getCurrentGroupDir() {
		return currentGroupDir;
	}
	public void setCurrentGroupDir(String currentGroupDir) {
		this.currentGroupDir = currentGroupDir;
	}
	
	/**当前组织 下级组织列表 List**/
	private List groupList;
	public List getGroupList() {
		return groupList;
	}
	public void setGroupList(List groupList) {
		this.groupList = groupList;
	}
	
	private JSONArray result;
	
	public JSONArray getResult() {
		return result;
	}
	public void setResult(JSONArray result) {
		this.result = result;
	}
	public String test(){
		//当前组织的下级组织
		try {
			
			List list = null;
			if(currentGroupId != null)
				list = groupservice.currentGroupChildes(currentGroupId, false);
			else{
				GroupEmployee employee = (GroupEmployee)ActionContext.getContext().getSession().get("user");
				if(employee.getGroup_srl() != null){
					list = groupservice.currentGroupChildes(employee.getGroup_srl(), true);
				}
			}
			
			JsonConfig config = new JsonConfig();
			config.setJsonPropertyFilter(new PropertyFilter() {
				//设置过滤TVenues和TPhaselangGrspcls属性
				@Override
				public boolean apply(Object source, String name, Object value){
				     return false;
				}
			});
			
			result = JSONArray.fromObject(list, config);

		}catch(Exception e){
			e.printStackTrace();
			results = ErrorCode.COMMON_ERROR;
		}

		return "success";
	}
	
	/**
	 * 查询组织列表
	 * <!-- 组织列表 --> struts service.xml 配置
	 * @return
	 */
	public String queryGroupList(){
		//当前登录组织
		GroupEmployee employee = (GroupEmployee)ActionContext.getContext().getSession().get("user");
		//操作人暂定员工编号
		OPERATOR=employee.getGrpemp_id();
		//当前组织ID
		if(employee.getGroup_srl() != null){
			currentGroupId = employee.getGroup_srl();
			currentGroupName = employee.getGroup_name();
		}

		//取出当前组织的级别
		try{
			Map mp = (Map) groupservice.getGroupLvlDirById(currentGroupId);
			currentGroupLvl = Integer.parseInt(mp.get("GROUP_LVL").toString());
		}catch(HandlerException ae){
			results = ae.toString();
		}catch(Exception e){
			e.printStackTrace();
			results = ErrorCode.COMMON_ERROR;
		}

		return "query";
	}

	//组织级别和父路径
	private Integer grouplvl;
	private String groupdir;
	public Integer getGrouplvl() {
		return grouplvl;
	}
	public void setGrouplvl(Integer grouplvl) {
		this.grouplvl = grouplvl;
	}
	public String getGroupdir() {
		return groupdir;
	}
	public void setGroupdir(String groupdir) {
		this.groupdir = groupdir;
	}
	
	//-- 新增组织:入口 struts service.xml 配置
	public String initAddGroupinfo(){
		
		return "addGroupinfo";
	}
	
	/**
	 * 增加组织
	 * @return
	 */
	public String addGroupinfo(){
		try{
			//超级管理员
			if(currentGroupId==null){
				//是否存在组织根节点,有则不能修改，无则新增
				if(!groupservice.hisGroupRoot()){
					addGroupRoot();
				}
			}
			else{
				//CODE不能重复，NAME不能重复
				GroupInfo ginfo= new GroupInfo();
				
				//取出当前组织的级别和路径
				Map mp = (Map) groupservice.getGroupLvlDirById(currentGroupId);
				currentGroupLvl = Integer.parseInt(mp.get("GROUP_LVL").toString());
				GROUP_LVL = currentGroupLvl + 1;
				currentGroupDir = mp.get("GROUP_DIR").toString();
				//组织根节点
				if (currentGroupDir.equals(";0;"))
					GROUP_DIR = ";" + currentGroupId.toString() + ";";
				else
					GROUP_DIR = currentGroupDir + currentGroupId.toString() + ";";
				
				ginfo.setUPGROUP(currentGroupId);
				ginfo.setGROUP_LVL(GROUP_LVL);
				ginfo.setGROUP_DIR(GROUP_DIR);
				ginfo.setGROUP_NEXT(0);
				
				ginfo.setGROUP_CODE(GROUP_CODE);
				ginfo.setGROUP_NAME(GROUP_NAME);
				ginfo.setGROUP_ADDR(GROUP_ADDR);
				ginfo.setGROUP_ZIP(GROUP_ZIP);
				ginfo.setGROUP_TEL_CN(GROUP_TEL_CN);
				ginfo.setGROUP_TEL_AREA(GROUP_TEL_AREA);
				ginfo.setGROUP_TEL(GROUP_TEL);
				ginfo.setGROUP_FAX_CN(GROUP_FAX_CN);
				ginfo.setGROUP_FAX_AREA(GROUP_FAX_AREA);
				ginfo.setGROUP_FAX(GROUP_FAX);
				ginfo.setGROUP_DESC(GROUP_DESC);
				ginfo.setINVALID(1);
				
				ginfo.setOPERATOR(OPERATOR);
				Date d=new Date();
				ginfo.setOPERATETIME(d);
				ginfo.setAUDITOR(AUDITOR);
				ginfo.setAUDITIME(d);
				ginfo.setAUDINOTE(AUDINOTE);
				ginfo.setAUDIRESULT(AUDIRESULT);
				ginfo.setSTATE(SysConstants.STATE_NORMAL);
				ginfo.setREMARK(REMARK);
				ginfo.setLANGCLASS(LANGCLASS); 
				
				//组织编号存在
				if(groupservice.groupCodeIsDup(GROUP_CODE)){
					results = "groupcodedup";
				}else{
					//组织名称存在
					if(groupservice.groupCodeIsDup(GROUP_NAME)){
						results = "groupnamedup";
					}else{
						//新增组织
						groupservice.addGroup(ginfo);
						//新增组织后，修改父组织状态为有子孙
						groupservice.updateGroupNext(currentGroupId);
						results = "success";
					}
				}
			}
		}catch(HandlerException ae){
			results = ae.toString();
		}catch(Exception e){
			e.printStackTrace();
			results = ErrorCode.COMMON_ERROR;
		}
		return "success";
	}
	//增加组织根节点 // 针对超级管理员
	private void addGroupRoot(){
		try {
			GroupInfo ginfo = new GroupInfo();
			ginfo.setUPGROUP(0L);
			ginfo.setGROUP_LVL(0);
			ginfo.setGROUP_DIR(";0;");
			ginfo.setGROUP_NEXT(0);
			
			ginfo.setGROUP_CODE(GROUP_CODE);
			ginfo.setGROUP_NAME(GROUP_NAME);
			ginfo.setGROUP_ADDR(GROUP_ADDR);
			ginfo.setGROUP_ZIP(GROUP_ZIP);
			ginfo.setGROUP_TEL_CN(GROUP_TEL_CN);
			ginfo.setGROUP_TEL_AREA(GROUP_TEL_AREA);
			ginfo.setGROUP_TEL(GROUP_TEL);
			ginfo.setGROUP_FAX_CN(GROUP_FAX_CN);
			ginfo.setGROUP_FAX_AREA(GROUP_FAX_AREA);
			ginfo.setGROUP_FAX(GROUP_FAX);
			ginfo.setGROUP_DESC(GROUP_DESC);
			ginfo.setINVALID(1);
			
			ginfo.setOPERATOR(OPERATOR);
			Date d=new Date();
			ginfo.setOPERATETIME(d);
			ginfo.setAUDITOR(AUDITOR);
			ginfo.setAUDITIME(d);
			ginfo.setAUDINOTE(AUDINOTE);
			ginfo.setAUDIRESULT(AUDIRESULT);
			ginfo.setSTATE(SysConstants.STATE_NORMAL);
			ginfo.setREMARK(REMARK);
			ginfo.setLANGCLASS(LANGCLASS); 

			groupservice.addGroup(ginfo);
			results = "success";
		} catch (HandlerException ae) {
			results = ae.toString();
		} catch (Exception e) {
			e.printStackTrace();
			results = ErrorCode.COMMON_ERROR;
		}
	}
	
	//<!-- 新增组织:新增 --> struts service.xml 配置
	private String results;
	public String getResults() {
		return results;
	}
	public void setResults(String results) {
		this.results = results;
	}
	

	//-- 浏览组织:入口 struts service.xml 配置
	public String viewGroupInfo(){
		try{
			GroupInfo groupinfo = groupservice.viewGroupInfo(viewGroupId);
			//设置界面的属性值
			setGroupInfoPropertiesValue(groupinfo);
		}catch(Exception e){
			e.printStackTrace();
			results = ErrorCode.COMMON_ERROR;
		}
		
		return "viewGroup";
	}

	/**
	 * 设置界面的属性值
	 * @param employee
	 */
	private void setGroupInfoPropertiesValue(GroupInfo groupinfo){
		GROUP_CODE = groupinfo.getGROUP_CODE();        
		GROUP_NAME = groupinfo.getGROUP_NAME();        
		GROUP_ADDR = groupinfo.getGROUP_ADDR();        
		GROUP_ZIP = groupinfo.getGROUP_ZIP();         
		GROUP_TEL_CN = groupinfo.getGROUP_TEL_CN();      
		GROUP_TEL_AREA = groupinfo.getGROUP_TEL_AREA();    
		GROUP_TEL = groupinfo.getGROUP_TEL();         
		GROUP_FAX_CN = groupinfo.getGROUP_FAX_CN();      
		GROUP_FAX_AREA = groupinfo.getGROUP_FAX_AREA();    
		GROUP_FAX = groupinfo.getGROUP_FAX();         
		GROUP_DESC = groupinfo.getGROUP_DESC();        
		REMARK = groupinfo.getREMARK(); 
	}
	
	
	//浏览组织ID
	private Long viewGroupId;
	public Long getViewGroupId() {
		return viewGroupId;
	}
	public void setViewGroupId(Long viewGroupId) {
		this.viewGroupId = viewGroupId;
	}

	/**
	 * 更新组织信息
	 * @return
	 */
	public String updateGroupInfo(){

		try{
			GroupInfo ginfo = new GroupInfo();
			ginfo.setSERIALID(SERIALID);
			ginfo.setGROUP_NAME(GROUP_NAME);
			ginfo.setGROUP_ADDR(GROUP_ADDR);
			ginfo.setGROUP_ZIP(GROUP_ZIP);
			ginfo.setGROUP_TEL_CN(GROUP_TEL_CN);
			ginfo.setGROUP_TEL_AREA(GROUP_TEL_AREA);
			ginfo.setGROUP_TEL(GROUP_TEL);
			ginfo.setGROUP_FAX_CN(GROUP_FAX_CN);
			ginfo.setGROUP_FAX_AREA(GROUP_FAX_AREA);
			ginfo.setGROUP_FAX(GROUP_FAX);
			ginfo.setGROUP_DESC(GROUP_DESC);
			ginfo.setOPERATOR(OPERATOR);
			Date d=new Date();
			ginfo.setOPERATETIME(d);
			ginfo.setREMARK(REMARK);

			//组织名称存在
			if(groupservice.groupCodeIsDup(GROUP_NAME)){
				results = "groupnamedup";
			}else{
				//修改组织
				groupservice.updateGroupInfo(ginfo);
				results = "success";
			}	
		}catch(Exception e){
			e.printStackTrace();
			results = ErrorCode.COMMON_ERROR;
		}
		
		return "success";
	}
	
	//新增属性，对应实体类
	private Long SERIALID;		//pk
	private Long UPGROUP;
	private Integer GROUP_LVL;
	private String GROUP_DIR;
	private String GROUP_CODE;
	private String GROUP_NAME;
	private String GROUP_ADDR;
	private String GROUP_ZIP;
	private String GROUP_TEL_CN;
	private String GROUP_TEL_AREA;
	private String GROUP_TEL;
	private String GROUP_FAX_CN;
	private String GROUP_FAX_AREA;
	private String GROUP_FAX;
	private String GROUP_DESC;
	private Integer INVALID;
	private String OPERATOR;
	private Date OPERATETIME;
	private String AUDITOR;
	private Date AUDITIME;
	private String AUDINOTE;
	private String AUDIRESULT;
	private Integer STATE;
	private String REMARK;
	private Long LANGCLASS;

	public Long getSERIALID() {
		return SERIALID;
	}
	public void setSERIALID(Long sERIALID) {
		SERIALID = sERIALID;
	}
	public Long getUPGROUP() {
		return UPGROUP;
	}
	public void setUPGROUP(Long uPGROUP) {
		UPGROUP = uPGROUP;
	}
	public Integer getGROUP_LVL() {
		return GROUP_LVL;
	}
	public void setGROUP_LVL(Integer gROUPLVL) {
		GROUP_LVL = gROUPLVL;
	}
	public String getGROUP_DIR() {
		return GROUP_DIR;
	}
	public void setGROUP_DIR(String gROUPDIR) {
		GROUP_DIR = gROUPDIR;
	}
	public String getGROUP_CODE() {
		return GROUP_CODE;
	}
	public void setGROUP_CODE(String gROUPCODE) {
		GROUP_CODE = gROUPCODE;
	}
	public String getGROUP_NAME() {
		return GROUP_NAME;
	}
	public void setGROUP_NAME(String gROUPNAME) {
		GROUP_NAME = gROUPNAME;
	}
	public String getGROUP_ADDR() {
		return GROUP_ADDR;
	}
	public void setGROUP_ADDR(String gROUPADDR) {
		GROUP_ADDR = gROUPADDR;
	}
	public String getGROUP_ZIP() {
		return GROUP_ZIP;
	}
	public void setGROUP_ZIP(String gROUPZIP) {
		GROUP_ZIP = gROUPZIP;
	}
	public String getGROUP_TEL_CN() {
		return GROUP_TEL_CN;
	}
	public void setGROUP_TEL_CN(String gROUPTELCN) {
		GROUP_TEL_CN = gROUPTELCN;
	}
	public String getGROUP_TEL_AREA() {
		return GROUP_TEL_AREA;
	}
	public void setGROUP_TEL_AREA(String gROUPTELAREA) {
		GROUP_TEL_AREA = gROUPTELAREA;
	}
	public String getGROUP_TEL() {
		return GROUP_TEL;
	}
	public void setGROUP_TEL(String gROUPTEL) {
		GROUP_TEL = gROUPTEL;
	}
	public String getGROUP_FAX_CN() {
		return GROUP_FAX_CN;
	}
	public void setGROUP_FAX_CN(String gROUPFAXCN) {
		GROUP_FAX_CN = gROUPFAXCN;
	}
	public String getGROUP_FAX_AREA() {
		return GROUP_FAX_AREA;
	}
	public void setGROUP_FAX_AREA(String gROUPFAXAREA) {
		GROUP_FAX_AREA = gROUPFAXAREA;
	}
	public String getGROUP_FAX() {
		return GROUP_FAX;
	}
	public void setGROUP_FAX(String gROUPFAX) {
		GROUP_FAX = gROUPFAX;
	}
	public String getGROUP_DESC() {
		return GROUP_DESC;
	}
	public void setGROUP_DESC(String gROUPDESC) {
		GROUP_DESC = gROUPDESC;
	}
	public Integer getINVALID() {
		return INVALID;
	}
	public void setINVALID(Integer iNVALID) {
		INVALID = iNVALID;
	}
	public String getOPERATOR() {
		return OPERATOR;
	}
	public void setOPERATOR(String oPERATOR) {
		OPERATOR = oPERATOR;
	}
	public Date getOPERATETIME() {
		return OPERATETIME;
	}
	public void setOPERATETIME(Date oPERATETIME) {
		OPERATETIME = oPERATETIME;
	}
	public String getAUDITOR() {
		return AUDITOR;
	}
	public void setAUDITOR(String aUDITOR) {
		AUDITOR = aUDITOR;
	}
	public Date getAUDITIME() {
		return AUDITIME;
	}
	public void setAUDITIME(Date aUDITIME) {
		AUDITIME = aUDITIME;
	}
	public String getAUDINOTE() {
		return AUDINOTE;
	}
	public void setAUDINOTE(String aUDINOTE) {
		AUDINOTE = aUDINOTE;
	}
	public String getAUDIRESULT() {
		return AUDIRESULT;
	}
	public void setAUDIRESULT(String aUDIRESULT) {
		AUDIRESULT = aUDIRESULT;
	}
	public Integer getSTATE() {
		return STATE;
	}
	public void setSTATE(Integer sTATE) {
		STATE = sTATE;
	}
	public String getREMARK() {
		return REMARK;
	}
	public void setREMARK(String rEMARK) {
		REMARK = rEMARK;
	}
	public Long getLANGCLASS() {
		return LANGCLASS;
	}
	public void setLANGCLASS(Long lANGCLASS) {
		LANGCLASS = lANGCLASS;
	}
}
